
package com.jf.cloud.product.feign;

import cn.hutool.core.collection.CollectionUtil;
import com.jf.cloud.api.delivery.feign.DeliveryFeignClient;
import com.jf.cloud.api.product.feign.ShopCartFeignClient;
import com.jf.cloud.api.product.feign.SpuFeignClient;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.order.bo.DeliveryModeBO;
import com.jf.cloud.common.order.vo.ShopCartItemVO;
import com.jf.cloud.common.product.constant.SpuType;
import com.jf.cloud.common.product.vo.SkuVO;
import com.jf.cloud.common.product.vo.SpuAndSkuVO;
import com.jf.cloud.common.product.vo.SpuVO;
import com.jf.cloud.common.response.ServerResponseEntity;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.common.util.Json;
import com.jf.cloud.product.service.ShopCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Hidden;

import java.util.List;
import java.util.Objects;

/**
 * @author zz
 * @date 2020/12/8
 */
@RestController
@Hidden
public class ShopCartFeignController implements ShopCartFeignClient {

    @Autowired
    private ShopCartService shopCartService;

    @Autowired
    private SpuFeignClient spuFeignClient;

    @Autowired
    private DeliveryFeignClient deliveryFeignClient;

    @Override
    public ServerResponseEntity<List<ShopCartItemVO>> getCheckedShopCartItems(Long addrId) {
        List<ShopCartItemVO> checkedShopCartItems = shopCartService.getCheckedShopCartItems();
        Long userId = AuthUserContext.get().getUserId();
        if (CollectionUtil.isEmpty(checkedShopCartItems)) {
            return ServerResponseEntity.success(checkedShopCartItems);
        }
        if (CollectionUtil.isNotEmpty(checkedShopCartItems)) {
            for (ShopCartItemVO shopCartItem : checkedShopCartItems) {
                shopCartItem.setDeliveryModeBO(Json.parseObject(shopCartItem.getDeliveryMode(), DeliveryModeBO.class));
                shopCartItem.setTotalAmount(shopCartItem.getCount() * shopCartItem.getSkuPriceFee());
                // 初始化商品实际金额
                shopCartItem.setActualTotal(shopCartItem.getTotalAmount());
                shopCartItem.setShareReduce(0L);
                // 处理下分类扣率和商品供应商信息
                ServerResponseEntity<SpuAndSkuVO> spuAndSkuResponse = spuFeignClient.getSpuAndSkuAndRateById(shopCartItem.getSpuId(),shopCartItem.getSkuId(),true);
                if (!spuAndSkuResponse.isSuccess()) {
                    throw new LuckException(spuAndSkuResponse.getMsg());
                }
                SkuVO sku = spuAndSkuResponse.getData().getSku();
                SpuVO spu = spuAndSkuResponse.getData().getSpu();
                shopCartItem.setRate(spu.getRate());
                shopCartItem.setSupplierRate(0.0);
                shopCartItem.setTotalPurchaseAmount(0L);
                shopCartItem.setSpuType(spu.getSpuType());
                // 判断下是否为供应商商品,如果是商家代销供应商商品且为供应商发货设置供应商信息
                if (!Objects.equals(shopCartItem.getSupplierId(),0L) && Objects.equals(shopCartItem.getSupplierDeliveryType(),1)) {
                    // 处理下供应商信息
                    shopCartItem.setSupplierId(spu.getSupplierId());
                    shopCartItem.setDeliveryTemplateId(spu.getDeliveryTemplateId());
                    shopCartItem.setSupplierPriceFee(sku.getSupplyPriceFee());
                    shopCartItem.setTotalPurchaseAmount(shopCartItem.getCount() * sku.getSupplyPriceFee());
                    shopCartItem.setSupplierSpuId(spu.getSupplierSpuId());
                    shopCartItem.setSupplierSkuId(sku.getSupplierSkuId());
                    shopCartItem.setSupplierRate(spu.getSupplierRate());
                } else{
                    shopCartItem.setSupplierId(0L);
                }
                //判断用户的默认地址是否在配送范围内
                if(Objects.equals(spu.getSpuType(), SpuType.ACTIVE.value()) || Objects.equals(shopCartItem.getSpuMold(), 1) || Objects.equals(userId, null)){
                    shopCartItem.setIsDelivery(true);
                }else {
                    shopCartItem.setIsDelivery(deliveryFeignClient.checkAddr(shopCartItem.getDeliveryTemplateId(), userId, addrId).getData());
                }
            }
        }
        return ServerResponseEntity.success(checkedShopCartItems);
    }

    @Override
    public ServerResponseEntity<Void> deleteItem(List<Long> shopCartItemIds) {
        Long userId = AuthUserContext.get().getUserId();
        shopCartService.deleteShopCartItemsByShopCartItemIds(userId,shopCartItemIds);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> updateIsClosedByShopIds(List<Long> shopIds, Integer isClose) {
        shopCartService.updateIsClosedByShopIds(shopIds, isClose);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> deleteCartByComboUpdate(List<Long> comboIdList, Long spuId, Long skuId) {
        shopCartService.deleteCartByComboUpdate(comboIdList,spuId,skuId);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> deleteCartByComboIdAndSpuIdS(Long comboId, List<Long> spuIdList) {
        shopCartService.deleteCartByComboIdAndSpuIdS(comboId,spuIdList);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> deleteCartByComboIdAndSkuIdS(Long comboId, List<Long> spuIdList) {
        shopCartService.deleteCartByComboIdAndSkuIdS(comboId,spuIdList);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<Void> updateCartSpuPriceByComboIdAndSkuId(Long comboId, Long skuId, Long price) {
        shopCartService.updateCartSpuPriceByComboIdAndSkuId(comboId,skuId,price);
        return ServerResponseEntity.success();
    }

    @Override
    public ServerResponseEntity<List<Long>> skuIdListByComboMainSkuId(List<Long> skuId) {
        List<Long> skuIdList = shopCartService.skuIdListByComboMainSkuId(skuId);
        return ServerResponseEntity.success(skuIdList);
    }

    @Override
    public ServerResponseEntity<Void> updateCartComboCount(Long comboId, Long spuId, Integer count) {
        shopCartService.updateCartComboCount(comboId,spuId,count);
        return ServerResponseEntity.success();
    }

}
